var inta = 0;        //跳跃幅度
var numinterval;     //跳跃幅度计数器
var postioninterval; //移动幅度计数器
var backinterval;    //归位计数器
var numtop = 350;    //起始Y 总：672
var numleft = 150;  //起始X  总：414
var numtype = 'left';//跳跃方向
var topcount = 0;    //归位计次
var targetX = 250;    //目标X位置
var targetY = 250;    //目标Y位置
var windowX = wx.getSystemInfoSync().windowWidth; //获取屏幕宽度
var windowY = wx.getSystemInfoSync().windowHeight;//获取屏幕高度
var rotatnum = 30;


var score = 0;//得分
var hiscore = 0;//最高得分

Page({
  /**
   * 页面的初始数据
   */
  data: {
    hidden:true,
    nocancel:true,
    motto: inta,
    top: numtop,
    left: numleft,
    taX: targetX,
    taY: targetY,
    tasrc: 'http://oq56tygic.bkt.clouddn.com/大滑稽.png',
    mysrc: 'http://oq56tygic.bkt.clouddn.com/喷水.png',
    myscore: score,
    myhiscore:hiscore,
    rotat: -rotatnum
  },

  /// 长按事件
  touchStart: function (e) {
    this.data.motto = inta;
    numinterval = setInterval(this.add, 100)
  },

  //弹起时间
  touchEnd: function (e) {
    topcount = inta;
    clearInterval(numinterval);
    //延时执行，产生动画效果
    postioninterval = setInterval(this.move,20)
  },

  //开始移动目标
  move() {
    //每移动完成一次，改变一次方向
    if(inta==0){
      if (numtype=='left'){
        numtype = 'right';
      }else{
        numtype = 'left';
      }
      //判断是否跳跃到指定位置
      if (targetX - numleft <= 20 && targetX - numleft >= -20){
        if (targetY - numtop <= 20 && targetY - numtop >= -20){
          var random = Math.floor(Math.random() * (8 - 5 + 1) + 5) * 10;

          if (targetX - numleft == 0 && targetY - numtop == 0){
            score = score + 5;
          }else{
            score = score + 1;
          }
        }else{
          //跳至结算页面
          this.setData({
            hidden:false
          })
        }
      }else{
        //跳至结算页面
        this.setData({
          hidden: false
        })
      }

      clearInterval(postioninterval);
      //复原目标的Y轴位置
      backinterval = setInterval(this.backY,20)


    }else{
      rotatnum = rotatnum + 30;
      numtop = numtop - 10;
      if(numtype == 'left') {
        numleft = numleft + 10;
      } else {
        numleft = numleft - 10;
      }
      inta--
      this.setData({
        rotat :rotatnum,
        motto :inta,
        top: numtop,
        left: numleft
      })

    }
  },

  confirm: function () {
    if (hiscore < score) {
      hiscore = score;
    }
    numtype = 'left';
    numtop = 350;
    numleft = 150;
    targetX = 250;
    targetY = 250;
    score = 0;
    this.setData({
      hidden: true,
      top:numtop,
      left:numleft,
      taY:targetY,
      taX:targetX,
      myscore:score,
      myhiscore:hiscore
    });

  },

  //累加力度
  add() {
    rotatnum = rotatnum+20;
    this.setData({
      rotat: rotatnum,
      motto: inta++
    })
  },

  //回归位置
  backY(){
    if(topcount==0){
      this.retarget();
      clearInterval(backinterval);
    }else{
    numtop = numtop + 10;
     this.setData({
       top: numtop,
     })
     topcount--;
    }
  },
  retarget(){
    var min = 3;
    var max = 0;
    if(numtype=='left'){
       max = (windowX - numleft) / 10 - 3;
    }else{
       max = numleft / 10 - 3;
      }

    if(max>=25){
      max=25;
    }

    var random = Math.floor(Math.random() * (max - min + 1) + min) * 10;

    if (numtype == 'left') {
      targetX = numleft + random;
    } else {
      targetX = numleft - random;
    }
    targetY = numtop - random;
    this.setData({
      myscore: score,
      taX: targetX,
      taY: targetY
    })
  }
})